package com.mvc.framework.validate;


public final class JsValidator {
	private JsValidator() {
	}



//	 private static Map<String, Map> allValidityRuleMap = new HashMap<String, Map>();
//	     private static Map<String, Map> allFieldDescMap = new HashMap<String, Map>();
//	     private static String jsValiditerFilePath;
//	     private static String[] pojoBeanPackageName;
//	    /**
//	      *
//	         将指定的包下边的所有Pojo类的标注信息一次性的读取到Map中，以备以后验证对象时使用
//	      */
//	     public static void loadAllClassAnnotationValidityInfoInPackage(String[] packageNameArr)
//	             throws IllegalArgumentException, SecurityException, IllegalAccessException, NoSuchMethodException,
//	             InvocationTargetException, ClassNotFoundException, IOException {
//	         for (int i = 0; i < packageNameArr.length; i++) {
//	             List<Class> classList = ReflectUtils.getAllClassInPackage(packageNameArr[i]);
//	             for (int j = 0; j < classList.size(); j++) {
//	                 Class currClass = classList.get(j);
//	                 String classFullName = currClass.getPackage().getName() + "." + currClass.getName();
//	                 Map[] validityRuleMapAndFieldDescMap = loadValidityRuleAndCreateValidityJsFile(currClass);
//	                 allValidityRuleMap.put(classFullName, validityRuleMapAndFieldDescMap[0]);
//	                 allFieldDescMap.put(classFullName, validityRuleMapAndFieldDescMap[1]);
//	             }
//	         }
//	     }
//	     /*
//	      * 对具体的一个类的标注做处理，读入字段描述　信息，读入非空验证信息，读入整数验证信息，并根据这些信息生成JS代码。
//	      */
//
//	     private static Map[] loadValidityRuleAndCreateValidityJsFile(Class currClass) throws IllegalArgumentException,
//	             IllegalAccessException, SecurityException, NoSuchMethodException, InvocationTargetException, IOException {
//	         StringBuffer validityCanNotNullJsStrBuf = new StringBuffer();
//	         StringBuffer validityIsIntegerJsStrBuf = new StringBuffer();
//	         Field[] fieldArr = currClass.getDeclaredFields();
//	         List<String> canNotNullFieldNameList = new ArrayList<String>();
//	         List<IsIntegerValiditerRuleInfo> isIntegerValiditerRuleInfoList = new ArrayList<IsIntegerValiditerRuleInfo>();
//	         Map<String, String> fieldDescMap = new HashMap<String, String>();
//	         for (int i = 0; i < fieldArr.length; i++) {
//	             String currFieldName = fieldArr[i].getName();
//	             //读入字段描述　信息
//	             FieldDesc fieldDescAnnotation = fieldArr[i].getAnnotation(FieldDesc.class);
//	             if (fieldDescAnnotation != null) {
//	                 String fieldDesc = fieldDescAnnotation.value();
//	                 System.out.println(fieldDesc);
//	                 fieldDescMap.put(currFieldName, fieldDesc);
//	             }
//	             else {
//	                 fieldDescMap.put(currFieldName, currFieldName);
//	             }
//	             //读入非空验证信息
//	             if (fieldArr[i].isAnnotationPresent(CanNotNull.class)) {
//	                 canNotNullFieldNameList.add(currFieldName);
//	                 validityCanNotNullJsStrBuf.append("if(document.all." + currFieldName + ".value.trim()==\"\"){alert('"
//	                         + fieldDescMap.get(currFieldName) + "不可以为空');}\n");
//	             }
//	             //读入整数验证信息
//	             IsInteger isIntegerAnnotation = fieldArr[i].getAnnotation(IsInteger.class);
//	             if (isIntegerAnnotation != null) {
//	                 int minWidth = isIntegerAnnotation.minWidth();
//	                 int maxWidth = isIntegerAnnotation.maxWidth();
//	                 int minValue = isIntegerAnnotation.minValue();
//	                 int maxValue = isIntegerAnnotation.maxValue();
//	                 IsIntegerValiditerRuleInfo isIntegerValiditerRuleInfo = new IsIntegerValiditerRuleInfo(currFieldName,
//	                         minWidth, maxWidth, minValue, maxValue);
//	                 isIntegerValiditerRuleInfoList.add(isIntegerValiditerRuleInfo);
//	                 if (minWidth > 0) {
//	                     validityIsIntegerJsStrBuf.append("if(document.all." + currFieldName
//	                             + ".value.length()<minWidth){alert('" + fieldDescMap.get(currFieldName) + "宽度不够！');}\n");
//	                 }
//	                 if (maxWidth < Integer.MAX_VALUE) {
//	                     validityIsIntegerJsStrBuf.append("if(document.all." + currFieldName
//	                             + ".value.length()>maxWidth){alert('" + fieldDescMap.get(currFieldName) + "宽度太长！');}\n");
//	                 }
//	                 if (minValue > Integer.MIN_VALUE) {
//	                     validityIsIntegerJsStrBuf.append("if(document.all." + currFieldName + ".value*1<minValue){alert('"
//	                             + fieldDescMap.get(currFieldName) + "的数值太小！');}\n");
//	                 }
//	                 if (maxValue < Integer.MAX_VALUE) {
//	                     validityIsIntegerJsStrBuf.append("if(document.all." + currFieldName + ".value*1>maxValue){alert('"
//	                             + fieldDescMap.get(currFieldName) + "的数值太大！');}\n");
//	                 }
//	             }
//	         }
//	         Map<String, List> classValidityRuleMap = new HashMap<String, List>();
//	         classValidityRuleMap.put("canNotNullFieldNameList", canNotNullFieldNameList);
//	         classValidityRuleMap.put("isIntegerValiditerRuleInfoList", isIntegerValiditerRuleInfoList);
//	         //根据这些信息生成JS代码
//	         BufferedWriter jsFileWriter = new BufferedWriter(new FileWriter(new File(jsValiditerFilePath
//	                 + java.io.File.separator + currClass.getName() + ".js")));
//	         jsFileWriter.write(validityCanNotNullJsStrBuf.toString());
//	         jsFileWriter.write(validityIsIntegerJsStrBuf.toString());
//	         jsFileWriter.close();
//	         return new Map[] { classValidityRuleMap, fieldDescMap };
//
//	     }
//	     /*
//	      * 具体的后台验证过程
//	      */
//
//	     public static ValidityResult validity(Object currObj) throws IllegalArgumentException, IllegalAccessException,
//	             SecurityException, NoSuchMethodException, InvocationTargetException {
//	         Class aClass = currObj.getClass();
//	         String classFullName = aClass.getPackage().getName() + "." + aClass.getName();
//	         Field[] fieldArr = aClass.getDeclaredFields();
//
//	         Map validityRuleMap = allValidityRuleMap.get(classFullName);
//	         Map fieldDescMap = allFieldDescMap.get(classFullName);
//	         List canNotNullFieldNameList = (List) validityRuleMap.get("canNotNullFieldNameList");
//	         List isIntegerValiditerRuleInfoList = (List) validityRuleMap.get("isIntegerValiditerRuleInfoList");
//	         ValidityResult validityResult = new ValidityResult();
//	         for (int j = 0; j < canNotNullFieldNameList.size(); j++) {
//	             String currFieldName = (String) canNotNullFieldNameList.get(j);
//	             String getMethodName = "get" + currFieldName.substring(0, 1).toUpperCase() + currFieldName.substring(1);
//	             Method getFieldValueMethod = aClass.getMethod(getMethodName);
//	             String fieldValue = ((String) getFieldValueMethod.invoke(currObj));
//	             if (fieldValue == null || fieldValue.equals("")) {
//	                 System.out.println(fieldDescMap.get(currFieldName) + " 不可以为空 ! ");
//	                 validityResult.addValidityInfo(fieldDescMap.get(currFieldName) + " 不可以为空 ! ");
//	             }
//	         }
//
//	         for (int j = 0; j < isIntegerValiditerRuleInfoList.size(); j++) {
//	             IsIntegerValiditerRuleInfo isIntegerValiditerRuleInfo = (IsIntegerValiditerRuleInfo) isIntegerValiditerRuleInfoList
//	                     .get(j);
//	             String currFieldName = isIntegerValiditerRuleInfo.getFieldName();
//	             String getMethodName = "get" + currFieldName.substring(0, 1).toUpperCase() + currFieldName.substring(1);
//	             Method getFieldValueMethod = aClass.getMethod(getMethodName);
//	             String fieldValue = ((String) getFieldValueMethod.invoke(currObj));
//	             int minWidth = isIntegerValiditerRuleInfo.getMinWidth();
//	             int maxWidth = isIntegerValiditerRuleInfo.getMaxWidth();
//	             int minValue = isIntegerValiditerRuleInfo.getMinValue();
//	             int maxValue = isIntegerValiditerRuleInfo.getMaxValue();
//
//	             if (StringUtils.isInteger(fieldValue)) {
//	                 if (fieldValue.length() < minWidth) {
//	                     validityResult.addValidityInfo(fieldDescMap.get(currFieldName) + " 宽度不可以小于 " + minWidth + "! ");
//	                 }
//	                 if (fieldValue.length() > maxWidth) {
//	                     validityResult.addValidityInfo(fieldDescMap.get(currFieldName) + " 宽度不可以大于 " + maxWidth + "! ");
//	                 }
//	                 if (Integer.parseInt(fieldValue) < minValue) {
//	                     validityResult.addValidityInfo(fieldDescMap.get(currFieldName) + " 的值不可以小于 " + minValue + "! ");
//	                 }
//	                 if (Integer.parseInt(fieldValue) > maxValue) {
//	                     validityResult.addValidityInfo(fieldDescMap.get(currFieldName) + " 的值不可以大于 " + maxValue + "! ");
//	                 }
//
//	             }
//	             else {
//
//	                 validityResult.addValidityInfo(fieldDescMap.get(currFieldName) + " 必须是整数格式 ! ");
//
//	             }
//	         }
//	         return validityResult;
//
//	     }
//
//	     public static String[] getPojoBeanPackageName() {
//	         return pojoBeanPackageName;
//	     }
//
//	     public static void setPojoBeanPackageName(String[] pojoBeanPackageName) {
//	         PojoBeanAnnotationValiditer.pojoBeanPackageName = pojoBeanPackageName;
//	     }
//
//	     public static String getJsValiditerFilePath() {
//	         return jsValiditerFilePath;
//	     }
//
//	     public static void setJsValiditerFilePath(String jsValiditerFilePath) {
//	         PojoBeanAnnotationValiditer.jsValiditerFilePath = jsValiditerFilePath;
//	     }
//
//	     public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException, SecurityException,
//	             NoSuchMethodException, InvocationTargetException, ClassNotFoundException, IOException {
//	         PojoBeanAnnotationValiditer.setPojoBeanPackageName(new String[] { "com.javaest.validity.test.pojo" });
//	         PojoBeanAnnotationValiditer.setJsValiditerFilePath("JS\\VALIDITY");
//	         PojoBeanAnnotationValiditer
//	                 .loadAllClassAnnotationValidityInfoInPackage(new String[] { "com.javaest.validity.test.pojo" });
//	         Student aStudent = new Student();
//	         aStudent.setAddress("吉林省长春市二道区");
//	         aStudent.setId("");
//	         aStudent.setAge("9");
//	         ValidityResult validityResult = PojoBeanAnnotationValiditer.validity(aStudent);
//	     }
//
//	 }
}
